package code.oldCode.classic150;

import com.sun.source.tree.Tree;
import utils.TreeNode;

import java.util.*;

/**
 * @author cuihaoran
 * @date 2025/1/6
 */
public class Classic_150binaryTreeLevelTraversal {

    /* 199. 二叉树的右视图 */
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if (root == null)
            return list;
        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.addLast(root);
        while(!deque.isEmpty()) {
            int nowLayerSize = deque.size();
            for (int i = 0; i < nowLayerSize; i++) {
                TreeNode node = deque.pollFirst();
                if (node.left != null)
                    deque.addLast(node.left);
                if (node.right != null)
                    deque.addLast(node.right);
                if (i == nowLayerSize - 1)
                    list.add(node.val);
            }
        }

        return list;
    }

    /* 637. 二叉树的层平均值 */
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> list = new ArrayList<>();
        if (root == null)
            return list;
        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.addLast(root);
        while (!deque.isEmpty()) {
            int size = deque.size();
            double sum = 0.0;
            for (int i = 0; i < size; i++) {
                TreeNode node = deque.pollFirst();
                sum += node.val;
                if (node.left != null)
                    deque.addLast(node.left);
                if (node.right != null)
                    deque.addLast(node.right);
            }
            list.add(sum / size);
        }

        return list;
    }

    /* 102. 二叉树的层序遍历 */
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> lists = new ArrayList<>();
        if (root == null)
            return lists;
        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.addLast(root);
        while (!deque.isEmpty()) {
            int size = deque.size();
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode node = deque.pollFirst();
                list.add(node.val);
                if (node.left != null)
                    deque.addLast(node.left);
                if (node.right != null)
                    deque.addLast(node.right);
            }
            lists.add(list);
        }

        return lists;
    }

    /* 103. 二叉树的锯齿形层序遍历 */
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> lists = new ArrayList<>();
        if (root == null)
            return lists;
        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.add(root);
        boolean leftToRight = true;
        while (!deque.isEmpty()) {
            int size = deque.size();
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                if (leftToRight) {
                    TreeNode node = deque.pollFirst();
                    list.add(node.val);
                    if (node.left != null)
                        deque.addLast(node.left);
                    if (node.right != null)
                        deque.addLast(node.right);
                } else {
                    TreeNode node = deque.pollLast();
                    list.add(node.val);
                    if (node.right != null)
                        deque.addFirst(node.right);
                    if (node.left != null)
                        deque.addFirst(node.left);
                }
            }
            lists.add(list);
            leftToRight = !leftToRight;
        }

        return lists;
    }

    /* 调试 */
    public static void main(String[] args) {

    }
}
